package lapsnake.model;

import java.beans.PropertyVetoException;
import java.util.ArrayList;




import java.util.List;

import org.python.antlr.ast.Interactive;
import org.python.antlr.ast.Module;
import org.python.antlr.ast.Suite;
import org.python.antlr.base.mod;
import org.python.antlr.base.stmt;

import lapsnake.model.data.ChildList;
import lapsnake.model.stmt.LapClassDef;
import lapsnake.py.ast.ASTHelperFactory;



/**This class wraps a module from the Python PythonTree and adds helper functionality such as retrieving a list of classes.*/
public final class LapModule extends LapNode {
	
	public final ChildList<LapStatement> body = new ChildList<>(this, LapStatement.class, true, false);
	
	public LapModule(PyAST pyAST, mod node) throws PropertyVetoException
	{
		super(pyAST, null, node);
		List<stmt> list;
		if(node instanceof Interactive)
			list = ((Interactive) node).getInternalBody();
		else if(node instanceof Module)
			list = ((Module) node).getInternalBody();
		else if(node instanceof Suite)
			list = ((Suite) node).getInternalBody();
		else 
			throw new RuntimeException(node+"("+node.getClass()+") cannot be the root of a module tree...");
		
		for(stmt child : list)
		{
			ASTHelperFactory.makeHelper(pyAST, body, child);
		}
		buildTextNodes(node);
	}
	
	public LapClassDef[] getClasses() {
		ArrayList<LapClassDef> classList = new ArrayList<>();
		
		for(LapStatement s : body)
		{
			if(s instanceof LapClassDef)
				classList.add((LapClassDef) s);
		}
		return classList.toArray(new LapClassDef[classList.size()]);
	}
	
}
